local skel = fk.CreateSkill {
  name = "lb__shengbian",
}
--[[
Fk:loadTranslationTable {
  ["lb__shengbian"] = "升变",
  --[":lb__shengbian"] = "出牌阶段开始时，你可以展示你与一名其他角色的手牌，然后其获得你的展示牌中有重复花色的牌各一张，你记录获得牌的花色直到下回合开始，其使用〖升变〗花色牌时，你移去此花色令之额外结算一次，因此造成伤害时，你摸一张牌并将伤害来源改为你。",
  --原描述
  --[":lb__shengbian"] = "出牌阶段开始时，你可以展示你与一名其他角色的手牌，令其获得你的展示牌中有重复花色的牌各一张，记录获得牌的花色直到下回合开始，其使用〖升变〗花色牌额外结算一次（每种花色限一次），因此造成伤害时，你摸一张牌并将伤害来源改为你。",
  --9.30
   --[":lb__shengbian"] = "出牌阶段开始时，你可以展示你与一名其他角色的手牌，令其获得你的展示牌中有重复花色的牌各一张，记录获得牌的花色直到下回合开始，其使用〖升变〗花色牌额外结算一次（每种花色限一次），此牌首次造成伤害时，你摸一张牌并将伤害来源改为你。",
  --10.3
  ["lb__shengbian"] = "升变",
   [":lb__shengbian"] = "出牌阶段开始时，你可以展示你与一名其他角色的手牌，令其获得你的展示牌中有重复花色的牌各一张，记录获得牌的花色直到下回合开始，其使用〖升变〗花色牌额外结算一次（每种花色限一次），此牌造成伤害时，你摸一张牌并将伤害来源改为你。",

  ["#lb__shengbian"] = "升变：你可以展示你与一名其他角色的手牌，然后其获得你的展示牌中有重复花色的牌各一张",
  ["#lb__shengbian-choose"] = "升变：获得%dest有重复花色的牌各一张",

  ["@lb__shengbian"] = "升变",
  ["@lb__shengbian_suit"] = "升变",

  ["$lb__shengbian1"] = "请你，以身入局吧。",
  ["$lb__shengbian2"] = "为我厮杀吧！",
}
--]]

Fk:loadTranslationTable {
  --10.3 升变改名擢英
  ["lb__shengbian"] = "擢英",
   [":lb__shengbian"] = "出牌阶段开始时，你可以展示你与一名其他角色的手牌，令其获得你的展示牌中有重复花色的牌各一张，记录获得牌的花色直到下回合开始，其使用〖擢英〗花色牌额外结算一次（每种花色限一次），此牌造成伤害时，你摸一张牌并将伤害来源改为你。",

  ["#lb__shengbian"] = "擢英：你可以展示你与一名其他角色的手牌，然后其获得你的展示牌中有重复花色的牌各一张",
  ["#lb__shengbian-choose"] = "擢英：获得%dest有重复花色的牌各一张",

  ["@lb__shengbian"] = "擢英",
  ["@lb__shengbian_suit"] = "擢英",

  ["$lb__shengbian1"] = "请你，以身入局吧。",
  ["$lb__shengbian2"] = "为我厮杀吧！",
}

Fk:addPoxiMethod { --涉猎
  name = "lb__shengbian",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2], to_select) then return true end
    local suit = Fk:getCardById(to_select).suit
    return table.every(data[2], function(id)
      return Fk:getCardById(id).suit ~= suit
    end)
  end,
  feasible = Util.TrueFunc,
}

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Play
        and table.find(player.room:getOtherPlayers(player), function(to)
          return (player:getHandcardNum() + to:getHandcardNum()) > 0
        end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = table.filter(player.room:getOtherPlayers(player), function(to)
      return (player:getHandcardNum() + to:getHandcardNum()) > 0
    end)
    if #tos > 0 then
      tos = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        cancelable = true,
        prompt = "#lb__shengbian",
      })
      event:setCostData(self, { tos = tos })
      return #tos > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    for _, show in ipairs({ player, to }) do
      if not show:isKongcheng() then
        show:showCards(show:getCardIds("h"))
      end
    end
    local give = player:getCardIds("h")
    if #give == 0 then return end
    local all = table.simpleClone(give)
    table.insertTableIfNeed(all, to:getCardIds("h"))
    local cards = {}
    for i = 1, 5, 1 do
      local ids = table.filter(all, function(id)
        return Fk:getCardById(id).suit == i
      end)
      if #ids > 1 then
        ids = table.filter(give, function(id)
          return table.contains(ids, id)
        end)
        if #ids > 0 then
          table.insertTableIfNeed(cards, ids)
        end
      end
    end
    if #cards == 0 then return end
    local get = {}
    for _, id in ipairs(cards) do
      local suit = Fk:getCardById(id).suit
      if table.every(get, function(id2)
            return Fk:getCardById(id2).suit ~= suit
          end) then
        table.insert(get, id)
      end
    end
    get = room:askToArrangeCards(to, {
      skill_name = skel.name,
      card_map = cards,
      prompt = "#lb__shengbian-choose::" .. player.id,
      free_arrange = false,
      box_size = 0,
      max_limit = { #cards, 5 },
      min_limit = { 0, #get },
      poxi_type = "lb__shengbian",
      default_choice = { {}, get },
    })[2]
    if #get > 0 then
      room:setPlayerMark(player, "@lb__shengbian", to)
      local suits = table.map(get, function(id)
        return Fk:getCardById(id):getSuitString(true)
      end)
      room:setPlayerMark(player, "@lb__shengbian_suit", suits)
      room:setPlayerMark(player, "lb__shengbian_suit", suits)
      room:obtainCard(to, get, true, fk.ReasonPrey, player, skel.name)
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player:getMark("@lb__shengbian")
        and table.contains(player:getTableMark("lb__shengbian_suit"), data.card:getSuitString(true))
  end,
  on_use = function(self, event, target, player, data)
    data.additionalEffect = (data.additionalEffect or 0) + 1
    data.extra_data = data.extra_data or {}
    data.extra_data.lb__shengbian = { data.additionalEffect + 1, player } --第几次结算发动升变，额外结算次数加上默认1次
    player.room:removeTableMark(player, "lb__shengbian_suit", data.card:getSuitString(true))
    player.room:removePlayerMark(player, "@lb__kansha")
  end,
})
--[[
skel:addEffect(fk.PreDamage, {
  is_delay_effect = true,
  can_refresh = function(self, event, target, player, data)
    local parent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true) --父事件
    return player:hasSkill(skel.name) and target ~= player
        and not (data.extra_data and data.extra_data.lb__shengbian_chain)
        and parent and parent.data and parent.data.Effect_index
        and parent.data.extra_data and parent.data.extra_data.lb__shengbian
        and type(parent.data.extra_data.lb__shengbian) == "table"
        and parent.data.extra_data.lb__shengbian[2] == player
        and parent.data.extra_data.lb__shengbian[1] == parent.data.Effect_index
  end,
  on_refresh = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.lb__shengbian_chain = player.id
    data.from = player
  end,
})

skel:addEffect(fk.DetermineDamageCaused, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
        and (data.skillName == skel.name..tostring(player.id) or
        (data.extra_data and data.extra_data.lb__shengbian_chain
        and data.extra_data.lb__shengbian_chain == player.id))
        and not data.prevented
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})
--]]

skel:addEffect(fk.DetermineDamageCaused, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local parent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true) --父事件
    if player:hasSkill(skel.name) --and target ~= player
    and not (data.extra_data and data.extra_data.lb__shengbian_chain)
    and parent and parent.data
    and parent.data.extra_data and parent.data.extra_data.lb__shengbian
    and type(parent.data.extra_data.lb__shengbian) == "table"
    and parent.data.extra_data.lb__shengbian[2] == player
    and not data.prevented then
      --parent.data.extra_data.lb__shengbian = nil  --10.3
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.lb__shengbian_chain = player.id
    data.from = player
    player:drawCards(1, skel.name)
  end,
})

skel:addEffect(fk.DamageFinished, {
  global = true,
  can_refresh = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.lb__shengbian_chain
        and data.extra_data.lb__shengbian_chain == player.id
        and data.beginnerOfTheDamage and data.chain_table
        and not data.prevented
  end,
  on_refresh = function(self, event, target, player, data)
    --data.skillName = skel.name..tostring(player.id) --淦，为什么传导伤害不记录起始伤害
    data.chain_table =
        table.filter(player.room:getOtherPlayers(data.to), function(p)
          return p.chained
        end)
  end,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@lb__shengbian", 0)
    room:setPlayerMark(player, "@lb__shengbian_suit", 0)
    room:setPlayerMark(player, "lb__shengbian_suit", 0)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
    player.room:setPlayerMark(player, "@lb__shengbian", 0)
    player.room:setPlayerMark(player, "@lb__shengbian_suit", 0)
    player.room:setPlayerMark(player, "lb__shengbian_suit", 0)
end)

return skel